package com.stellar.circle.domain.sPostLike.impl;

import cn.hutool.core.util.ObjectUtil;
import com.stellar.auth.constants.ErrorCodeConstants;
import com.stellar.circle.basic.entity.SPost;
import com.stellar.circle.basic.entity.SPostLike;
import com.stellar.circle.basic.service.SPostLikeService;
import com.stellar.circle.basic.service.SPostService;
import com.stellar.circle.controller.admin.sPostLike.vo.SPostLikeRequestPageVO;
import com.stellar.circle.controller.admin.sPostLike.vo.SPostLikeRequestVO;
import com.stellar.circle.controller.admin.sPostLike.vo.SPostLikeResponseVO;
import com.stellar.circle.convert.SPostLikeConvert;
import com.stellar.circle.domain.sPostLike.SPostLikeDomainService;

import com.stellar.circle.enums.PostLikeTypeEnums;
import com.stellar.core.context.LoginContextHolder;
import com.stellar.web.core.bean.PageResult;
import com.stellar.web.core.enums.IsDeletedFlagEnum;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

import static com.stellar.core.util.ServiceExceptionUtil.exception;

/**
 * 帖子点赞表领域服务实现
 *
 * @author Jqchan
 * @version 1.0
 * @date 2025-05-19 14:23:05
 */
@Service
public class SPostLikeDomainServiceImpl implements SPostLikeDomainService {

    @Resource
    private SPostLikeService sPostLikeService;

    @Resource
    private SPostService sPostService;

    @Override
    public PageResult<SPostLikeResponseVO> page(SPostLikeRequestPageVO requestPageVO) {
        PageResult<SPostLikeResponseVO> pageResult = new PageResult<>();
        SPostLike queryEntity = SPostLikeConvert.INSTANCE.convert(requestPageVO);
        queryEntity.setIsDeleted(IsDeletedFlagEnum.UN_DELETED.getVal());

        List<SPostLike> entities = sPostLikeService.page(queryEntity,
                requestPageVO.getStart(), requestPageVO.getPageSize());

        if (entities.isEmpty()) {
            return pageResult;
        }

        Long count = sPostLikeService.count(queryEntity);
        List<SPostLikeResponseVO> vos = SPostLikeConvert.INSTANCE.convertEntityToRespList(entities);

        pageResult.setResult(vos);
        pageResult.setTotal(count.intValue());
        return pageResult;
    }

    @Override
    public Boolean update(SPostLikeRequestVO requestVO) {
        SPostLike entity = SPostLikeConvert.INSTANCE.convert(requestVO);
        return sPostLikeService.update(entity) != 0;
    }

    @Override
    public Boolean delete(Long id) {
        SPostLike entity = new SPostLike();
        entity.setId(id);
        entity.setIsDeleted(IsDeletedFlagEnum.DELETED.getVal());
        return sPostLikeService.update(entity) != 0;
    }

    @Override
    public Boolean insert(SPostLikeRequestVO requestVO) {
        SPostLike entity = SPostLikeConvert.INSTANCE.convert(requestVO);
        return sPostLikeService.insert(entity) != 0;
    }

    @Override
    public SPostLikeResponseVO get(Long id) {
        SPostLike entity = sPostLikeService.queryById(id);
        return SPostLikeConvert.INSTANCE.convert(entity);
    }

    /**
     * 点赞 - 待优化
     * 1.缓存点赞数，定时任务刷做持久化
     * 2.加锁防止多线程并发
     *
     * @param postId
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean like(Long postId) {
        try {
            String userId = LoginContextHolder.getLoginId();
            if (StringUtils.isBlank(userId)) {
                throw exception(ErrorCodeConstants.AUTH_USER_NOT_LOGIN);
            }
            SPostLike queryEntity = new SPostLike();
            queryEntity.setLikeType(PostLikeTypeEnums.LIKE.getType());
            queryEntity.setPostId(postId);
            queryEntity.setUserId(Long.valueOf(userId));
            queryEntity.setIsDeleted(IsDeletedFlagEnum.UN_DELETED.getVal());
            SPostLike sPostLike = sPostLikeService.queryByCondition(queryEntity);
            SPost sPost = sPostService.queryById(postId);
            if (ObjectUtil.isNull(sPostLike)) {
                sPostLikeService.insert(queryEntity);
                sPost.setLikeCount(sPost.getLikeCount() + 1);
            }
            if (IsDeletedFlagEnum.UN_DELETED.getVal().equals(sPostLike.getIsDeleted())) {
                sPostLike.setIsDeleted(IsDeletedFlagEnum.DELETED.getVal());
                sPostLikeService.update(sPostLike);
                sPost.setLikeCount(sPost.getLikeCount() - 1);
            }
            return true;
        } catch (Exception e) {
            return false;
        }
    }
}
